home *** CD-ROM | disk | FTP | other *** search
/ Sky at Night 2007 June / SAN CD 6-2007 CD-ROM 25.iso / pc / Software / AstroGrav_Win / Java / jre1.6.0 / lib / rt.jar / javax / swing / JComboBox.class (.txt) < prev    next >
Encoding:
Java Class File  |  2006-11-29  |  12.9 KB  |  643 lines

  1. package javax.swing;
  2.  
  3. import java.awt.AWTEvent;
  4. import java.awt.Component;
  5. import java.awt.EventQueue;
  6. import java.awt.ItemSelectable;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.awt.event.InputEvent;
  10. import java.awt.event.ItemEvent;
  11. import java.awt.event.ItemListener;
  12. import java.awt.event.KeyEvent;
  13. import java.beans.PropertyChangeListener;
  14. import java.io.IOException;
  15. import java.io.ObjectOutputStream;
  16. import java.util.Vector;
  17. import javax.accessibility.Accessible;
  18. import javax.accessibility.AccessibleContext;
  19. import javax.swing.event.ListDataEvent;
  20. import javax.swing.event.ListDataListener;
  21. import javax.swing.event.PopupMenuEvent;
  22. import javax.swing.event.PopupMenuListener;
  23. import javax.swing.plaf.ComboBoxUI;
  24. import javax.swing.plaf.UIResource;
  25.  
  26. public class JComboBox extends JComponent implements ItemSelectable, ListDataListener, ActionListener, Accessible {
  27.    private static final String uiClassID = "ComboBoxUI";
  28.    protected ComboBoxModel dataModel;
  29.    protected ListCellRenderer renderer;
  30.    protected ComboBoxEditor editor;
  31.    protected int maximumRowCount = 8;
  32.    protected boolean isEditable = false;
  33.    protected KeySelectionManager keySelectionManager = null;
  34.    protected String actionCommand = "comboBoxChanged";
  35.    protected boolean lightWeightPopupEnabled = JPopupMenu.getDefaultLightWeightPopupEnabled();
  36.    protected Object selectedItemReminder = null;
  37.    private Object prototypeDisplayValue;
  38.    private boolean firingActionEvent = false;
  39.    private boolean selectingItem = false;
  40.    private Action action;
  41.    private PropertyChangeListener actionPropertyChangeListener;
  42.  
  43.    public JComboBox(ComboBoxModel var1) {
  44.       this.setModel(var1);
  45.       this.init();
  46.    }
  47.  
  48.    public JComboBox(Object[] var1) {
  49.       this.setModel(new DefaultComboBoxModel(var1));
  50.       this.init();
  51.    }
  52.  
  53.    public JComboBox(Vector<?> var1) {
  54.       this.setModel(new DefaultComboBoxModel(var1));
  55.       this.init();
  56.    }
  57.  
  58.    public JComboBox() {
  59.       this.setModel(new DefaultComboBoxModel());
  60.       this.init();
  61.    }
  62.  
  63.    private void init() {
  64.       this.installAncestorListener();
  65.       this.setOpaque(true);
  66.       this.updateUI();
  67.    }
  68.  
  69.    protected void installAncestorListener() {
  70.       this.addAncestorListener(new 1(this));
  71.    }
  72.  
  73.    public void setUI(ComboBoxUI var1) {
  74.       super.setUI(var1);
  75.    }
  76.  
  77.    public void updateUI() {
  78.       this.setUI((ComboBoxUI)UIManager.getUI(this));
  79.       ListCellRenderer var1 = this.getRenderer();
  80.       if (!(var1 instanceof UIResource) && var1 instanceof Component) {
  81.          SwingUtilities.updateComponentTreeUI((Component)var1);
  82.       }
  83.  
  84.    }
  85.  
  86.    public String getUIClassID() {
  87.       return "ComboBoxUI";
  88.    }
  89.  
  90.    public ComboBoxUI getUI() {
  91.       return (ComboBoxUI)this.ui;
  92.    }
  93.  
  94.    public void setModel(ComboBoxModel var1) {
  95.       ComboBoxModel var2 = this.dataModel;
  96.       if (var2 != null) {
  97.          var2.removeListDataListener(this);
  98.       }
  99.  
  100.       this.dataModel = var1;
  101.       this.dataModel.addListDataListener(this);
  102.       this.selectedItemReminder = this.dataModel.getSelectedItem();
  103.       this.firePropertyChange("model", var2, this.dataModel);
  104.    }
  105.  
  106.    public ComboBoxModel getModel() {
  107.       return this.dataModel;
  108.    }
  109.  
  110.    public void setLightWeightPopupEnabled(boolean var1) {
  111.       boolean var2 = this.lightWeightPopupEnabled;
  112.       this.lightWeightPopupEnabled = var1;
  113.       this.firePropertyChange("lightWeightPopupEnabled", var2, this.lightWeightPopupEnabled);
  114.    }
  115.  
  116.    public boolean isLightWeightPopupEnabled() {
  117.       return this.lightWeightPopupEnabled;
  118.    }
  119.  
  120.    public void setEditable(boolean var1) {
  121.       boolean var2 = this.isEditable;
  122.       this.isEditable = var1;
  123.       this.firePropertyChange("editable", var2, this.isEditable);
  124.    }
  125.  
  126.    public boolean isEditable() {
  127.       return this.isEditable;
  128.    }
  129.  
  130.    public void setMaximumRowCount(int var1) {
  131.       int var2 = this.maximumRowCount;
  132.       this.maximumRowCount = var1;
  133.       this.firePropertyChange("maximumRowCount", var2, this.maximumRowCount);
  134.    }
  135.  
  136.    public int getMaximumRowCount() {
  137.       return this.maximumRowCount;
  138.    }
  139.  
  140.    public void setRenderer(ListCellRenderer var1) {
  141.       ListCellRenderer var2 = this.renderer;
  142.       this.renderer = var1;
  143.       this.firePropertyChange("renderer", var2, this.renderer);
  144.       this.invalidate();
  145.    }
  146.  
  147.    public ListCellRenderer getRenderer() {
  148.       return this.renderer;
  149.    }
  150.  
  151.    public void setEditor(ComboBoxEditor var1) {
  152.       ComboBoxEditor var2 = this.editor;
  153.       if (this.editor != null) {
  154.          this.editor.removeActionListener(this);
  155.       }
  156.  
  157.       this.editor = var1;
  158.       if (this.editor != null) {
  159.          this.editor.addActionListener(this);
  160.       }
  161.  
  162.       this.firePropertyChange("editor", var2, this.editor);
  163.    }
  164.  
  165.    public ComboBoxEditor getEditor() {
  166.       return this.editor;
  167.    }
  168.  
  169.    public void setSelectedItem(Object var1) {
  170.       Object var2 = this.selectedItemReminder;
  171.       Object var3 = var1;
  172.       if (var2 == null || !var2.equals(var1)) {
  173.          if (var1 != null && !this.isEditable()) {
  174.             boolean var4 = false;
  175.  
  176.             for(int var5 = 0; var5 < this.dataModel.getSize(); ++var5) {
  177.                Object var6 = this.dataModel.getElementAt(var5);
  178.                if (var1.equals(var6)) {
  179.                   var4 = true;
  180.                   var3 = var6;
  181.                   break;
  182.                }
  183.             }
  184.  
  185.             if (!var4) {
  186.                return;
  187.             }
  188.          }
  189.  
  190.          this.selectingItem = true;
  191.          this.dataModel.setSelectedItem(var3);
  192.          this.selectingItem = false;
  193.          if (this.selectedItemReminder != this.dataModel.getSelectedItem()) {
  194.             this.selectedItemChanged();
  195.          }
  196.       }
  197.  
  198.       this.fireActionEvent();
  199.    }
  200.  
  201.    public Object getSelectedItem() {
  202.       return this.dataModel.getSelectedItem();
  203.    }
  204.  
  205.    public void setSelectedIndex(int var1) {
  206.       int var2 = this.dataModel.getSize();
  207.       if (var1 == -1) {
  208.          this.setSelectedItem((Object)null);
  209.       } else {
  210.          if (var1 < -1 || var1 >= var2) {
  211.             throw new IllegalArgumentException("setSelectedIndex: " + var1 + " out of bounds");
  212.          }
  213.  
  214.          this.setSelectedItem(this.dataModel.getElementAt(var1));
  215.       }
  216.  
  217.    }
  218.  
  219.    public int getSelectedIndex() {
  220.       Object var1 = this.dataModel.getSelectedItem();
  221.       int var2 = 0;
  222.  
  223.       for(int var3 = this.dataModel.getSize(); var2 < var3; ++var2) {
  224.          Object var4 = this.dataModel.getElementAt(var2);
  225.          if (var4 != null && var4.equals(var1)) {
  226.             return var2;
  227.          }
  228.       }
  229.  
  230.       return -1;
  231.    }
  232.  
  233.    public Object getPrototypeDisplayValue() {
  234.       return this.prototypeDisplayValue;
  235.    }
  236.  
  237.    public void setPrototypeDisplayValue(Object var1) {
  238.       Object var2 = this.prototypeDisplayValue;
  239.       this.prototypeDisplayValue = var1;
  240.       this.firePropertyChange("prototypeDisplayValue", var2, var1);
  241.    }
  242.  
  243.    public void addItem(Object var1) {
  244.       this.checkMutableComboBoxModel();
  245.       ((MutableComboBoxModel)this.dataModel).addElement(var1);
  246.    }
  247.  
  248.    public void insertItemAt(Object var1, int var2) {
  249.       this.checkMutableComboBoxModel();
  250.       ((MutableComboBoxModel)this.dataModel).insertElementAt(var1, var2);
  251.    }
  252.  
  253.    public void removeItem(Object var1) {
  254.       this.checkMutableComboBoxModel();
  255.       ((MutableComboBoxModel)this.dataModel).removeElement(var1);
  256.    }
  257.  
  258.    public void removeItemAt(int var1) {
  259.       this.checkMutableComboBoxModel();
  260.       ((MutableComboBoxModel)this.dataModel).removeElementAt(var1);
  261.    }
  262.  
  263.    public void removeAllItems() {
  264.       this.checkMutableComboBoxModel();
  265.       MutableComboBoxModel var1 = (MutableComboBoxModel)this.dataModel;
  266.       int var2 = var1.getSize();
  267.       if (var1 instanceof DefaultComboBoxModel) {
  268.          ((DefaultComboBoxModel)var1).removeAllElements();
  269.       } else {
  270.          for(int var3 = 0; var3 < var2; ++var3) {
  271.             Object var4 = var1.getElementAt(0);
  272.             var1.removeElement(var4);
  273.          }
  274.       }
  275.  
  276.       this.selectedItemReminder = null;
  277.       if (this.isEditable()) {
  278.          this.editor.setItem((Object)null);
  279.       }
  280.  
  281.    }
  282.  
  283.    void checkMutableComboBoxModel() {
  284.       if (!(this.dataModel instanceof MutableComboBoxModel)) {
  285.          throw new RuntimeException("Cannot use this method with a non-Mutable data model.");
  286.       }
  287.    }
  288.  
  289.    public void showPopup() {
  290.       this.setPopupVisible(true);
  291.    }
  292.  
  293.    public void hidePopup() {
  294.       this.setPopupVisible(false);
  295.    }
  296.  
  297.    public void setPopupVisible(boolean var1) {
  298.       this.getUI().setPopupVisible(this, var1);
  299.    }
  300.  
  301.    public boolean isPopupVisible() {
  302.       return this.getUI().isPopupVisible(this);
  303.    }
  304.  
  305.    public void addItemListener(ItemListener var1) {
  306.       this.listenerList.add(ItemListener.class, var1);
  307.    }
  308.  
  309.    public void removeItemListener(ItemListener var1) {
  310.       this.listenerList.remove(ItemListener.class, var1);
  311.    }
  312.  
  313.    public ItemListener[] getItemListeners() {
  314.       return (ItemListener[])this.listenerList.getListeners(ItemListener.class);
  315.    }
  316.  
  317.    public void addActionListener(ActionListener var1) {
  318.       this.listenerList.add(ActionListener.class, var1);
  319.    }
  320.  
  321.    public void removeActionListener(ActionListener var1) {
  322.       if (var1 != null && this.getAction() == var1) {
  323.          this.setAction((Action)null);
  324.       } else {
  325.          this.listenerList.remove(ActionListener.class, var1);
  326.       }
  327.  
  328.    }
  329.  
  330.    public ActionListener[] getActionListeners() {
  331.       return (ActionListener[])this.listenerList.getListeners(ActionListener.class);
  332.    }
  333.  
  334.    public void addPopupMenuListener(PopupMenuListener var1) {
  335.       this.listenerList.add(PopupMenuListener.class, var1);
  336.    }
  337.  
  338.    public void removePopupMenuListener(PopupMenuListener var1) {
  339.       this.listenerList.remove(PopupMenuListener.class, var1);
  340.    }
  341.  
  342.    public PopupMenuListener[] getPopupMenuListeners() {
  343.       return (PopupMenuListener[])this.listenerList.getListeners(PopupMenuListener.class);
  344.    }
  345.  
  346.    public void firePopupMenuWillBecomeVisible() {
  347.       Object[] var1 = this.listenerList.getListenerList();
  348.       PopupMenuEvent var2 = null;
  349.  
  350.       for(int var3 = var1.length - 2; var3 >= 0; var3 -= 2) {
  351.          if (var1[var3] == PopupMenuListener.class) {
  352.             if (var2 == null) {
  353.                var2 = new PopupMenuEvent(this);
  354.             }
  355.  
  356.             ((PopupMenuListener)var1[var3 + 1]).popupMenuWillBecomeVisible(var2);
  357.          }
  358.       }
  359.  
  360.    }
  361.  
  362.    public void firePopupMenuWillBecomeInvisible() {
  363.       Object[] var1 = this.listenerList.getListenerList();
  364.       PopupMenuEvent var2 = null;
  365.  
  366.       for(int var3 = var1.length - 2; var3 >= 0; var3 -= 2) {
  367.          if (var1[var3] == PopupMenuListener.class) {
  368.             if (var2 == null) {
  369.                var2 = new PopupMenuEvent(this);
  370.             }
  371.  
  372.             ((PopupMenuListener)var1[var3 + 1]).popupMenuWillBecomeInvisible(var2);
  373.          }
  374.       }
  375.  
  376.    }
  377.  
  378.    public void firePopupMenuCanceled() {
  379.       Object[] var1 = this.listenerList.getListenerList();
  380.       PopupMenuEvent var2 = null;
  381.  
  382.       for(int var3 = var1.length - 2; var3 >= 0; var3 -= 2) {
  383.          if (var1[var3] == PopupMenuListener.class) {
  384.             if (var2 == null) {
  385.                var2 = new PopupMenuEvent(this);
  386.             }
  387.  
  388.             ((PopupMenuListener)var1[var3 + 1]).popupMenuCanceled(var2);
  389.          }
  390.       }
  391.  
  392.    }
  393.  
  394.    public void setActionCommand(String var1) {
  395.       this.actionCommand = var1;
  396.    }
  397.  
  398.    public String getActionCommand() {
  399.       return this.actionCommand;
  400.    }
  401.  
  402.    public void setAction(Action var1) {
  403.       Action var2 = this.getAction();
  404.       if (this.action == null || !this.action.equals(var1)) {
  405.          this.action = var1;
  406.          if (var2 != null) {
  407.             this.removeActionListener(var2);
  408.             var2.removePropertyChangeListener(this.actionPropertyChangeListener);
  409.             this.actionPropertyChangeListener = null;
  410.          }
  411.  
  412.          this.configurePropertiesFromAction(this.action);
  413.          if (this.action != null) {
  414.             if (!this.isListener(ActionListener.class, this.action)) {
  415.                this.addActionListener(this.action);
  416.             }
  417.  
  418.             this.actionPropertyChangeListener = this.createActionPropertyChangeListener(this.action);
  419.             this.action.addPropertyChangeListener(this.actionPropertyChangeListener);
  420.          }
  421.  
  422.          this.firePropertyChange("action", var2, this.action);
  423.       }
  424.  
  425.    }
  426.  
  427.    private boolean isListener(Class var1, ActionListener var2) {
  428.       boolean var3 = false;
  429.       Object[] var4 = this.listenerList.getListenerList();
  430.  
  431.       for(int var5 = var4.length - 2; var5 >= 0; var5 -= 2) {
  432.          if (var4[var5] == var1 && var4[var5 + 1] == var2) {
  433.             var3 = true;
  434.          }
  435.       }
  436.  
  437.       return var3;
  438.    }
  439.  
  440.    public Action getAction() {
  441.       return this.action;
  442.    }
  443.  
  444.    protected void configurePropertiesFromAction(Action var1) {
  445.       AbstractAction.setEnabledFromAction(this, var1);
  446.       AbstractAction.setToolTipTextFromAction(this, var1);
  447.       this.setActionCommandFromAction(var1);
  448.    }
  449.  
  450.    protected PropertyChangeListener createActionPropertyChangeListener(Action var1) {
  451.       return new ComboBoxActionPropertyChangeListener(this, var1);
  452.    }
  453.  
  454.    protected void actionPropertyChanged(Action var1, String var2) {
  455.       if (var2 == "ActionCommandKey") {
  456.          this.setActionCommandFromAction(var1);
  457.       } else if (var2 == "enabled") {
  458.          AbstractAction.setEnabledFromAction(this, var1);
  459.       } else if ("ShortDescription" == var2) {
  460.          AbstractAction.setToolTipTextFromAction(this, var1);
  461.       }
  462.  
  463.    }
  464.  
  465.    private void setActionCommandFromAction(Action var1) {
  466.       this.setActionCommand(var1 != null ? (String)var1.getValue("ActionCommandKey") : null);
  467.    }
  468.  
  469.    protected void fireItemStateChanged(ItemEvent var1) {
  470.       Object[] var2 = this.listenerList.getListenerList();
  471.  
  472.       for(int var3 = var2.length - 2; var3 >= 0; var3 -= 2) {
  473.          if (var2[var3] == ItemListener.class) {
  474.             ((ItemListener)var2[var3 + 1]).itemStateChanged(var1);
  475.          }
  476.       }
  477.  
  478.    }
  479.  
  480.    protected void fireActionEvent() {
  481.       if (!this.firingActionEvent) {
  482.          this.firingActionEvent = true;
  483.          ActionEvent var1 = null;
  484.          Object[] var2 = this.listenerList.getListenerList();
  485.          long var3 = EventQueue.getMostRecentEventTime();
  486.          int var5 = 0;
  487.          AWTEvent var6 = EventQueue.getCurrentEvent();
  488.          if (var6 instanceof InputEvent) {
  489.             var5 = ((InputEvent)var6).getModifiers();
  490.          } else if (var6 instanceof ActionEvent) {
  491.             var5 = ((ActionEvent)var6).getModifiers();
  492.          }
  493.  
  494.          for(int var7 = var2.length - 2; var7 >= 0; var7 -= 2) {
  495.             if (var2[var7] == ActionListener.class) {
  496.                if (var1 == null) {
  497.                   var1 = new ActionEvent(this, 1001, this.getActionCommand(), var3, var5);
  498.                }
  499.  
  500.                ((ActionListener)var2[var7 + 1]).actionPerformed(var1);
  501.             }
  502.          }
  503.  
  504.          this.firingActionEvent = false;
  505.       }
  506.  
  507.    }
  508.  
  509.    protected void selectedItemChanged() {
  510.       if (this.selectedItemReminder != null) {
  511.          this.fireItemStateChanged(new ItemEvent(this, 701, this.selectedItemReminder, 2));
  512.       }
  513.  
  514.       this.selectedItemReminder = this.dataModel.getSelectedItem();
  515.       if (this.selectedItemReminder != null) {
  516.          this.fireItemStateChanged(new ItemEvent(this, 701, this.selectedItemReminder, 1));
  517.       }
  518.  
  519.    }
  520.  
  521.    public Object[] getSelectedObjects() {
  522.       Object var1 = this.getSelectedItem();
  523.       if (var1 == null) {
  524.          return new Object[0];
  525.       } else {
  526.          Object[] var2 = new Object[]{var1};
  527.          return var2;
  528.       }
  529.    }
  530.  
  531.    public void actionPerformed(ActionEvent var1) {
  532.       Object var2 = this.getEditor().getItem();
  533.       this.setPopupVisible(false);
  534.       this.getModel().setSelectedItem(var2);
  535.       String var3 = this.getActionCommand();
  536.       this.setActionCommand("comboBoxEdited");
  537.       this.fireActionEvent();
  538.       this.setActionCommand(var3);
  539.    }
  540.  
  541.    public void contentsChanged(ListDataEvent var1) {
  542.       Object var2 = this.selectedItemReminder;
  543.       Object var3 = this.dataModel.getSelectedItem();
  544.       if (var2 == null || !var2.equals(var3)) {
  545.          this.selectedItemChanged();
  546.          if (!this.selectingItem) {
  547.             this.fireActionEvent();
  548.          }
  549.       }
  550.  
  551.    }
  552.  
  553.    public void intervalAdded(ListDataEvent var1) {
  554.       if (this.selectedItemReminder != this.dataModel.getSelectedItem()) {
  555.          this.selectedItemChanged();
  556.       }
  557.  
  558.    }
  559.  
  560.    public void intervalRemoved(ListDataEvent var1) {
  561.       this.contentsChanged(var1);
  562.    }
  563.  
  564.    public boolean selectWithKeyChar(char var1) {
  565.       if (this.keySelectionManager == null) {
  566.          this.keySelectionManager = this.createDefaultKeySelectionManager();
  567.       }
  568.  
  569.       int var2 = this.keySelectionManager.selectionForKey(var1, this.getModel());
  570.       if (var2 != -1) {
  571.          this.setSelectedIndex(var2);
  572.          return true;
  573.       } else {
  574.          return false;
  575.       }
  576.    }
  577.  
  578.    public void setEnabled(boolean var1) {
  579.       super.setEnabled(var1);
  580.       this.firePropertyChange("enabled", !this.isEnabled(), this.isEnabled());
  581.    }
  582.  
  583.    public void configureEditor(ComboBoxEditor var1, Object var2) {
  584.       var1.setItem(var2);
  585.    }
  586.  
  587.    public void processKeyEvent(KeyEvent var1) {
  588.       if (var1.getKeyCode() == 9) {
  589.          this.hidePopup();
  590.       }
  591.  
  592.       super.processKeyEvent(var1);
  593.    }
  594.  
  595.    public void setKeySelectionManager(KeySelectionManager var1) {
  596.       this.keySelectionManager = var1;
  597.    }
  598.  
  599.    public KeySelectionManager getKeySelectionManager() {
  600.       return this.keySelectionManager;
  601.    }
  602.  
  603.    public int getItemCount() {
  604.       return this.dataModel.getSize();
  605.    }
  606.  
  607.    public Object getItemAt(int var1) {
  608.       return this.dataModel.getElementAt(var1);
  609.    }
  610.  
  611.    protected KeySelectionManager createDefaultKeySelectionManager() {
  612.       return new DefaultKeySelectionManager(this);
  613.    }
  614.  
  615.    private void writeObject(ObjectOutputStream var1) throws IOException {
  616.       var1.defaultWriteObject();
  617.       if (this.getUIClassID().equals("ComboBoxUI")) {
  618.          byte var2 = JComponent.getWriteObjCounter(this);
  619.          --var2;
  620.          JComponent.setWriteObjCounter(this, var2);
  621.          if (var2 == 0 && this.ui != null) {
  622.             this.ui.installUI(this);
  623.          }
  624.       }
  625.  
  626.    }
  627.  
  628.    protected String paramString() {
  629.       String var1 = this.selectedItemReminder != null ? this.selectedItemReminder.toString() : "";
  630.       String var2 = this.isEditable ? "true" : "false";
  631.       String var3 = this.lightWeightPopupEnabled ? "true" : "false";
  632.       return super.paramString() + ",isEditable=" + var2 + ",lightWeightPopupEnabled=" + var3 + ",maximumRowCount=" + this.maximumRowCount + ",selectedItemReminder=" + var1;
  633.    }
  634.  
  635.    public AccessibleContext getAccessibleContext() {
  636.       if (this.accessibleContext == null) {
  637.          this.accessibleContext = new AccessibleJComboBox(this);
  638.       }
  639.  
  640.       return this.accessibleContext;
  641.    }
  642. }
  643.